<template>
  <el-container>
    <el-header>
      <el-form :inline="true" :model="device" class="demo-form-line">
        <el-form-item label="设备编号">
          <el-input v-model="device.no"></el-input>
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input v-model="device.name"></el-input>
        </el-form-item>
         <el-form-item label="资产编号">
          <el-input v-model="device.assetNo"></el-input>
        </el-form-item>
        <el-form-item label="设备状态">
          <el-select v-model="device.status" placeholder="设备状态">
            <el-option label="正常" value="1"></el-option>
            <el-option label="停机" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-header>

    <el-main>
      <el-button icon="el-icon-edit" type="text" @click="createDevice">添加设备</el-button>
      <el-button icon="el-icon-date" type="text">保养周期</el-button>
      <el-table :data="devices" style="width: 100%" class="el-table el-table--border">
        <el-table-column fixed prop="deviceNo" label="设备编号" width="150"></el-table-column>
        <el-table-column fixed prop="deviceName" label="设备名称" width="120"></el-table-column>
        <el-table-column fixed prop="deviceStatus" label="设备状态" width="120"></el-table-column>
        <el-table-column fixed prop="deviceAssetNo" label="资产编号" width="120"></el-table-column>
        <el-table-column fixed prop="deviceNameplateNo" label="铭牌号" width="300"></el-table-column>
        <el-table-column prop="deviceProducer" label="设备制造商" width="120"></el-table-column>
        <el-table-column prop="deviceSeat" label="设备位置" width="120"></el-table-column>
        <el-table-column prop="deviceManager" label="负责人" width="120"></el-table-column>
        <el-table-column prop="deviceManagerDepartment" label="负责部门" width="120"></el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="item">
            <el-button @click="showDetail(item)" type="text" size="small">明细</el-button>
            <el-button @click="editDevice(item)" type="text" size="small">编辑</el-button>
          </template>
        </el-table-column>
        </el-table>
    </el-main>
     <el-footer>
       <div class="block">
        <span class="demonstration">显示总数</span>
        <el-pagination layout="prev, pager, next" :total="total" :page-size="limit" @current-change="handleCurrentChange"></el-pagination>
       </div>
     </el-footer>
  </el-container>
</template>

<style lang="scss" scoped>
  section {
    padding-top: 20px;
  }
</style>

<script>
import * as deviceApi from "@/api/device/index"

function getCustormData(data, key) {
  return data[key];
}

export default {
  data() {
   return {
     device: {
       deviceNo: '',
       deviceName: '',
       deviceStatus: ''
     },
     devices: [],
     total: 0,
     limit: 20
   }
  },
  methods: {
    search () {
      
    },
    showDetail (data) {
      const row = getCustormData(data, 'row')
      console.log(row)
    },
    createDevice () {
      this.$router.push({path: 'info', query: {id: 0}})
    },
    editDevice (data) {
      const row = getCustormData(data, 'row')
      const id = row.id

      this.$router.push({path: 'info', query: {id: id}})
    },
    getDevices (page) {
      const limit = 20
      const params = {page: page, limit: this.limit}
      deviceApi.getDevices(params).then(res => {
        if(res.code === 200) {
          this.devices = getCustormData(res.data, 'rows')
          this.total = getCustormData(res.data, 'total')
        }
      })
    },
    handleCurrentChange(val) {
      this.getDevices(val)
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.getDevices(1)
    })
  },
  activated () {
    
  }
}
</script>

